package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.scene;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneService;
import com.iwhalecloud.bss.kite.client.api.settle.IOperateSettlementService;
import com.iwhalecloud.bss.kite.client.vo.scene.OfferInstModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.SceneVO;
import com.iwhalecloud.bss.kite.client.vo.settle.PreviewSceneVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.goods.IGoodsPriceFactorService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.PriceFactorsVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.handler.scene.AbstractSceneHandler;
import com.ztesoft.bss.base.staticdata.CoreDataOffer;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <Description> <br>
 * 套餐新装场景处理
 *
 * @author zhu.zixin<br>
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.scene <br>
 * @since R9.0<br>
 */
@Service
public class LocalNewBuyOfferSceneHandler extends AbstractSceneHandler {

    @Autowired
    private IQuerySceneService querySceneService;

    @Autowired
    private IGoodsPriceFactorService goodsPriceFactorService;

    @Override
    public void execute(AcceptContext context) {
        String offerInstId = "";
        Offer offer = OfferCache.get(context.getAcceptRequest().getOfferId());
        // 基础销售品不生成销售品实例
        if (!KiteObjectUtils.isEmpty(offer) && !CoreDataOffer.OFF_0005_10.equals(offer.getOfferType())) {
            offerInstId = querySceneService.getSceneInst(context.getAcceptRequest().getSceneInstId(), context.getAcceptRequest().getCustId()).getRootInstId();
        }

        context.getAcceptRequest().setOfferInstId(offerInstId);
    }

    @Override
    public boolean check(AcceptContext context) {
        return true;
    }

    @Override
    public SceneVO initSceneVO(AcceptContext context) {
        return new SceneNewOfferSeneVO();
    }

    /**
     * 所有执行后再组装
     *
     * @param context
     */
    @Override
    public void afterExecute(AcceptContext context) {
        OfferInstModuleVO offerInstModuleVO = new OfferInstModuleVO();
        if (!KiteObjectUtils.isEmpty(context) && !KiteObjectUtils.isEmpty(context.getSceneVo())) {
            SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
            if (!KiteObjectUtils.isEmpty(sceneNewOfferSeneVO) && KiteObjectUtils
                .isEmpty(sceneNewOfferSeneVO.getSceneProductModuleVO())) {
                offerInstModuleVO = sceneNewOfferSeneVO.getSceneProductModuleVO().getOffer();
            }
            // 组装计价因子返回给前端
            PriceFactorsVO priceFactorsVO = new PriceFactorsVO();
            priceFactorsVO.setPriceFactors(goodsPriceFactorService.getPriceFactors(context.getAcceptRequest().getOfferId()));
            sceneNewOfferSeneVO.setPriceFactorsVO(priceFactorsVO);
        }
        if (!KiteObjectUtils.isEmpty(offerInstModuleVO) && KiteListUtils
            .isNotEmpty(offerInstModuleVO.getOfferInstList())) {
            offerInstModuleVO.getOfferInstList().get(0).setOfferMember(
                genOfferInst(((SceneNewOfferSeneVO) context.getSceneVo()).getSceneProductModuleVO().getProduct()));
        }

    }

    @Override
    public PreviewSceneVO previewScene(String sceneInstId) {
        IOperateSettlementService operateSettlementService = SpringUtil.getBean(IOperateSettlementService.class);
        return operateSettlementService.generatePreviewSceneVOForOfferAccept(sceneInstId).getResultObject();
    }

}
